<?php
include '../includes/db.php';

$user = $_POST['user'] ?? '';
$action = $_POST['action'] ?? 'show';
$score = $_POST['score'] ?? 60;

// 增加用户
if ($action === 'add') {
    $sql = "INSERT INTO count (user, count, used) VALUES (?, 0, 1)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    echo "用户已添加。";
}

// 增加分数
if ($action === 'add_score') {
    $sql = "UPDATE count SET count = count + ? WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("is", $score, $user);
    $stmt->execute();
    echo "分数已增加。";
}

// 扣分
if ($action === 'subtract_score') {
    $sql = "UPDATE count SET count = count - ? WHERE user = ? AND count >= ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iis", $score, $user, $score);
    $stmt->execute();
    echo "分数已扣除。";
}

// 设置分数
if ($action === 'set_score') {
    $sql = "UPDATE count SET count = ? WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("is", $score, $user);
    $stmt->execute();
    echo "分数已设置。";
}

// 隐藏用户
if ($action === 'hide') {
    $sql = "UPDATE count SET used = 0 WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    echo "用户已隐藏。";
}

// 显示用户
if ($action === 'show') {
    $sql = "UPDATE count SET used = 1 WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    echo "用户已显示。";
}
if ($action === 'setadmin') {
    $sql = "UPDATE count SET adminable = 1 WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    echo "用户已设置";
}

// 显示用户
if ($action === 'resetadmin') {
    $sql = "UPDATE count SET adminable = 0 WHERE user = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    echo "用户已取消";
}
